package com.starry.home.ui.login

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.airbnb.lottie.LottieAnimationView
import com.starry.core.base.BaseActivity
import com.starry.core.constant.GlobalConfig
import com.starry.core.utils.SpUtil
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivitySplashBinding
import com.starry.home.ui.main.MainActivity
import com.starry.home.viewmodel.login.SplashViewModel

/**
 * Package:com.starry.dotpen_patients.ui.login
 * Author:starr
 * Time:2024/1/3 16:56
 * Description: 闪屏页面
 */
@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {
    private lateinit var mLottieView: LottieAnimationView
    private var userName: String? = null
    private var userPassword: String? = null

    companion object {
        fun startAction(context: Context) {
            val intent = Intent(context, SplashActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initData() {
        userName = SpUtil.getStringValue(GlobalConfig.userPhone)
        userPassword = SpUtil.getStringValue(GlobalConfig.userPassword)
    }

    override fun initView() {
        ZKStatusBarUtil.setTransparentForWindow(this)
        ZKStatusBarUtil.setDarkMode(this)
        mLottieView = mDataBinding.splashLottie
        mLottieView.addAnimatorListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                mLottieView.removeAnimatorListener(this)

                if (userName.isNullOrBlank() || userPassword.isNullOrBlank()) {
                    //去登录页面
                    val bundle = Bundle()
                    bundle.putBoolean("isLogin", true)
                    LoginActivity.startAction(this@SplashActivity, bundle)
                } else {
                    MainActivity.startAction(this@SplashActivity)
                }
                finish()
            }
        })
    }


    override fun getLayoutId() = R.layout.activity_splash
}